package lz.controls.nickScroll
{
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	
	import lz.controls.LScrollBar;
	import lz.controls.events.ScrollEvent;
	
	public class ScrollBarByMc extends Sprite
	{
		private var _scrollBar:LScrollBar;
		
		/**
		 *容器 
		 */
		private var _contain:Sprite;
		/**
		 *目标容器 
		 */
		private var _target:Sprite;
		/**
		 *内容高度 
		 */
		private var _targetHeight:int;
		
		public function ScrollBarByMc()
		{
			super();
		}
		
		
		/**
		 * 设置大小 
		 * @param $width
		 * @param $height
		 * 
		 */
		public function setSize($width:int,$height:int,$style:String = null):void
		{
			if(_scrollBar != null)return;
			_scrollBar = new LScrollBar();
			if($style != null)
				_scrollBar.skin = $style;
			_contain = new Sprite();
			this.addChild(_scrollBar);
			this.addChild(_contain);
			_scrollBar.x = $width + 2;
			_scrollBar.height = $height;
			_contain.scrollRect = new Rectangle(0,0,$width,$height);
//			var shap:Shape = new Shape();
//			shap.graphics.clear();
//			shap.graphics.beginFill(0x000000);
//			shap.graphics.drawRect(0,0,$width,$height);
//			shap.graphics.endFill();
//			_contain.addChild(shap);
//			_contain.mask = shap;
			
			_scrollBar.addEventListener(ScrollEvent.SCROLL,onHandler,false,0,true);
			this.addEventListener(MouseEvent.MOUSE_WHEEL,onWheelHandler);
		}
		protected function onHandler(event:ScrollEvent):void
		{
			//trace("_target"+_target.height);
			_target.y = -(_targetHeight -_scrollBar.height) * (event.position /_scrollBar.maxScrollPosition );
		}
		/**
		 *鼠标滚动轴事件  
		 * @param event
		 * 
		 */
		private function onWheelHandler(event:MouseEvent):void
		{
			_scrollBar.scrollPosition -= event.delta*_scrollBar.lineScrollSize ;
			
		}
		/**
		 * 设置目标容器 
		 * @param $target
		 * 
		 */
		public function set target($target:Sprite):void
		{
			_target = $target;
			_contain.addChild(_target);
			if(_target.height <= _scrollBar.height)_scrollBar.alpha = 0;
		}
		/**
		 *更新 滚动内容和滚动条滑块的大小及位置
		 * 
		 */
		public function upData():void
		{
			//trace("_target"+_target.height);
			goToStart();
			_scrollBar.maxScrollPosition =_targetHeight - _scrollBar.height;
			if(_targetHeight <= _scrollBar.height)
				_scrollBar.alpha = 0;
			else
				_scrollBar.alpha = 1;
			_target.y = -(_targetHeight -_scrollBar.height) * (_scrollBar.scrollPosition /_scrollBar.maxScrollPosition );
		}
		/**
		 * 最顶端
		 * 
		 */
		public function goToStart():void
		{
			_scrollBar.scrollPosition = 0;
			_target.y = 0;
		}
		/**
		 * 最低端 
		 * 
		 */
		public function goToLast():void
		{
			_scrollBar.maxScrollPosition = _targetHeight - _scrollBar.height;
		}
		/**
		 *内容容器高度 
		 * @param $value
		 * 
		 */
		public function set targetHeight($value:int):void
		{
			_targetHeight = $value;
		}
		public function get targetHeight():int
		{
			return _targetHeight;
		}
		/**
		 * 
		 * 
		 */
		public function destroyView():void
		{
			if(this.parent)this.parent.removeChild(this);
			_scrollBar.removeEventListener(ScrollEvent.SCROLL,onHandler);
			this.removeEventListener(MouseEvent.MOUSE_WHEEL,onWheelHandler);
			_scrollBar.destroy();
			_scrollBar = null;
			_contain = null;
			_target = null;
		}
		
	}
}